Date.prototype.format = function(format){ 
    var o =  { 
    "M+" : this.getMonth()+1, //month 
    "d+" : this.getDate(), //day 
    "h+" : this.getHours(), //hour 
    "m+" : this.getMinutes(), //minute 
    "s+" : this.getSeconds(), //second 
    "q+" : Math.floor((this.getMonth()+3)/3), //quarter 
    "S" : this.getMilliseconds() //millisecond 
    };
    if(/(y+)/.test(format)){ 
    	format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
    }
    for(var k in o)  { 
	    if(new RegExp("("+ k +")").test(format)){ 
	    	format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length)); 
	    }
    }
    return format; 
};

var HX = HONGXIN = {
	// 编辑器参数
	kingEditorParams : {
		
		//指定上传文件参数名称
		filePostName  : "uploadFile",
		//下面这行代码就是关键的所在，当失去焦点时执行 this.sync();
		afterBlur: function () { this.sync(); },
		//指定上传文件请求的url。
		uploadJson : '../../file/upload.do',
		//上传类型，分别为image、flash、media、file
		//dir : "image"
		//items: ["source", "|", "undo", "redo", "|", "preview", "print", "template", "code", "cut", "copy", "paste", "plainpaste", "wordpaste", "|", "justifyleft", "justifycenter", "justifyright", "justifyfull", "insertorderedlist", "insertunorderedlist", "indent", "outdent", "subscript", "superscript", "clearhtml", "quickformat", "selectall", "|", "fullscreen", "/", "formatblock", "fontname", "fontsize", "|", "forecolor", "hilitecolor", "bold", "italic", "underline", "strikethrough", "lineheight", "removeformat", "|", "image", "multiimage", "flash", "media", "insertfile", "table", "hr", "emoticons", "baidumap", "pagebreak", "anchor", "link", "unlink", "|", "about"]
		items : [ "source", "|", "undo", "redo", "|", "preview", "print",
				"cut", "copy", "paste", "plainpaste",
				"wordpaste", "|", "justifyleft", "justifycenter",
				"justifyright", "justifyfull", "insertorderedlist",
				"insertunorderedlist", "indent", "outdent", "subscript",
				"superscript", "clearhtml", "quickformat", "selectall", "|",
				"fullscreen", "/", "formatblock", "fontname", "fontsize", "|",
				"forecolor", "hilitecolor", "bold", "italic", "underline",
				"strikethrough", "lineheight", "removeformat", "|",
				"multiimage", "media", "table", "hr", "baidumap", "link",
				"unlink"]
	},
	
	// 标签分类活动
	activeType : function(val, row) {
		if(val == null) {
			return "";
		}
		if(val=='0'){
			return "月度";
		}
		if(val=='1'){
			return "季度";
		}
		if(val=="2"){
			return "年度";
		}
		if(val=="3"){
			return "企业家夫人";
		}
	},
	
	//用户状态
	userType : function(val, row) {
		if(val=='0'){
			return "注册成功";
		}
		if(val=='1'){
			return "邮件激活成功";
		}
		if(val=="9"){
			return "注册审核不通过";
		}
		if(val=="10"){
			return "成为用户";
		}
		if(val=='15'){
			return "下载入会申请";
		}
		if(val=='20'){
			return "上传入会资料";
		}
		if(val=="24"){
			return "入会资料审核不通过";
		}
		if(val=="25"){
			return "入会资料审核通过";
		}
		if(val=='30'){
			return "下载入会协议";
		}
		if(val=='33'){
			return "上传入会协议";
		}
		if(val=="34"){
			return "缴纳会费成功";
		}
		if(val=="35"){
			return "上传回执单";
		}
		if(val=='39'){
			return "回执单审核不通过";
		}
		if(val=="40"){
			return "回执单审核通过";
		}
		if(val=="45"){
			return "<span style='color:red;'>领取证书，流程结束</span>";
		}
		
	},
	activeClass : function(val, row) {
		if(val == null) {
			return "";
		}
		if(val == '0') {
			return '普通会员';
		}
		if(val=='1'){
			return "一般会员";
		}
		if(val=='2'){
			return "VIP会员";
		}
		if(val=="3"){
			return "理事会员";
		}
	},
	
	// 格式化时间
	formatDateTime : function(val,row){
		if(val == null) {
			return "";
		}
		var now = new Date(val);
    	return now.format("yyyy-MM-dd hh:mm:ss");
	},
		
	// 格式化连接
	formatUrl : function(val,row){
		if(val){
			return "<button type='button' onclick=register_check("+"'"+val+"'"+",1)>通过</button><button type='button' onclick=register_check("+"'"+val+"'"+",0)>拒绝</button>";
		}
	},
	
	// 查看文件
	viewFile : function(val,row){
		if(val){
			return "<button type='button' onclick=viewFile("+"'"+val+"'"+")>查看文件</button>";
		}
	},
	// 确认缴费
	makeSurePay : function(val,row){
		if(val){
			return "<button type='button' onclick=makeSurePay11("+"'"+val+"'"+")>确认缴费</button>";
		}
	},
	goCheckDetail1 : function(val, row) {
		if(val) {
			return "<a href='javascript:;' onClick=window.parent.click_me('"+ "admin/activemanager/active_checkdetail_list.do?id=" + row.id + "&type=1',"+"'普通会员报名"+row.activeName+"情况详情'"+")>" + val + "</a>";
		}
		return val;
	},
	goCheckDetail2 : function(val, row) {
		if(val) {
			return "<a href='javascript:;' onClick=window.parent.click_me('"+ "admin/activemanager/active_checkdetail_list.do?id=" + row.id + "&type=2',"+"'VIP会员报名"+row.activeName+"情况详情'"+")>" + val + "</a>";
		}
		return val;
	},
	goCheckDetail3 : function(val, row) {
		if(val) {
			return "<a href='javascript:;' onClick=window.parent.click_me('"+ "admin/activemanager/active_checkdetail_list.do?id=" + row.id + "&type=3',"+"'权益理事会员报名"+row.activeName+"情况详情'"+")>" + val + "</a>";
		}
		return val;
	},
	goCheckDetail4 : function(val, row) {
		if(val) {
			return "<a href='javascript:;' onClick=window.parent.click_me('"+ "admin/activemanager/active_checkdetail_list.do?id=" + row.id + "&type=4',"+"'全部报名"+row.activeName+"情况详情'"+")>" + val + "</a>";
		}
		return val;
	},
	// 格式化还原按钮
	register_restore : function(val,row){
		if(val){
			return "<button type='button' onclick=register_restore("+"'"+val+"'"+",3)>还原</button>";
		}
	},
	// 发放证书
	fafang_fadf : function(val,row){
		if(val){
			return "<button type='button' onclick=register_restore("+"'"+val+"'"+",3)>已发放证书</button>";
		}
	},
	// 下载按钮
	downloadButton : function(val,row){
		if(val){
			return "<button type='button' onclick=dowload_file("+"'"+val+"'"+")>下载</button>";
		}
	},
	
	
	
	
	// 格式化状态信息
	formatStatus : function(val,row){
        if (val == 1){
            return '<span style="color:green;">是</span>';
        } else if(val == 0){
        	return '<span style="color:red;">否</span>';
        } else {
        	return '未知';
        }
    },
    
 // 格式化入会形式
    joinType : function(val,row){
    	if (val == 1){
            return '<span style="color:#4876FF;">个人形式</span>';
        } else if(val == 2){
        	return '<span style="color:#8B1A1A;">公司形式</span>';
        } else {
        	return '未知';
        }
    },
    // 格式化会员等级
    levelClass : function(val,row){
    	if (val == 1){
    		return '<span style="color:#CD2990;">一般会员</span>';
    	} else if(val == 2){
    		return '<span style="color:#BF3EFF;">VIP会员</span>';
    	}else if(val == 3){
    		return '<span style="color:#8B0A50;">理事会员</span>';
    	} else {
    		return '<span style="color:red;">不是会员</span>';
    	}
    },
    
    init : function(data){
    	// 初始化图片上传组件
    	this.initPicUpload(data);
    	// 初始化选择类目组件
    	this.initArticleCat(data);
    },
    // 初始化图片上传组件
    initPicUpload : function(data){
    	
    	$(".picFileUpload").each(function(i,e){
    		var _ele = $(e);
    		_ele.siblings("div.pics").remove();
    		_ele.after('\
    			<div class="pics">\
        			<ul></ul>\
        		</div>');
    		// 回显图片
        	if(data && data.pics){
        		var imgs = data.pics.split(",");
        		for(var i in imgs){
        			if($.trim(imgs[i]).length > 0){
        				_ele.siblings(".pics").find("ul").append("<li><a href='"+imgs[i]+"' target='_blank'><img src='"+imgs[i]+"' width='80' height='50' /></a></li>");
        			}
        		}
        	}
        	//给“上传图片按钮”绑定click事件
        	$(e).click(function(){
        		var form = $(this).parentsUntil("form").parent("form");
        		//打开图片上传窗口
        		KindEditor.editor(HX.kingEditorParams).loadPlugin('multiimage',function(){
        			var editor = this;
        			editor.plugin.multiImageDialog({
						clickFn : function(urlList) {
							var imgArray = [];
							
							KindEditor.each(urlList, function(i, data) {
								imgArray.push(data.url);
								form.find(".pics ul").append("<li><a href='"+data.url+"' target='_blank'><img src='"+data.url+"' width='80' height='50' /></a></li>");
							});

							//imgUrl
							//判断img标签是否有图片
							if($("#imgUrl").val()==''){
								//没有img
								form.find("[name=imgUrl]").val(imgArray.join(","));
							}else{
								//a,b,c,d,e
								var newImg = imgArray.join(",");
								var oldImg = form.find("[name=imgUrl]").val();
								form.find("[name=imgUrl]").val(oldImg + "," + newImg);
							}
							
							editor.hideDialog();
						}
					});
        		});
        	});
    	});
    	
    	
    	
    },
    

        
    // 初始化选择类目组件
    initArticleCat : function(data){
//    	console.info(data)
    	$(".selectNewsCat").each(function(i,e){
    		var _ele = $(e);
    		if(data && data.cid){
    			_ele.after("<span style='margin-left:10px;'>"+data.cid+"</span>");
    		}else{
    			_ele.after("<span style='margin-left:10px;'></span>");
    		}
    		_ele.unbind('click').click(function(){
    			$("<div>").css({padding:"5px"}).html("<ul>")
    			.window({
    				width:'500',
    			    height:"450",
    			    modal:true,
    			    closed:true,
    			    iconCls:'icon-save',
    			    title:'选择文章分类',
    			    onOpen : function(){
    			    	var _win = this;
    			    	$("ul",_win).tree({
    			    		url:'../../admin/article/cat/list.do',
    			    		animate:true,
    			    		onClick : function(node){
    			    			if($(this).tree("isLeaf",node.target)){
    			    				// 填写到cid中
    			    				_ele.parent().find("[name=cid]").val(node.id);
    			    				_ele.next().text(node.text).attr("cid",node.id);
    			    				$(_win).window('close');
    			    				if(data && data.fun){
    			    					data.fun.call(this,node);
    			    				}
    			    			}
    			    		}
    			    	});
    			    },
    			    onClose : function(){
    			    	$(this).window("destroy");
    			    }
    			}).window('open');
    		});
    	});
    },
    
    createEditor : function(select){
    	return KindEditor.create(select, HX.kingEditorParams);
    },
    
    
    createWindow : function(params){
    	$("<div>").css({padding:"5px"}).window({
    		width : "1000px",
    		height :"500px",
    		modal:true,
    		title : params.title?params.title:" ",
    		href : params.url,
		    onClose : function(){
		    	$(this).window("destroy");
		    },
		    onLoad : function(){
		    	if(params.onLoad){
		    		params.onLoad.call(this);
		    	}
		    }
    	}).window("open");
    },
    
    closeCurrentWindow : function(){
    	$(".panel-tool-close").click();
    },
    
    
    /**
     * 获取数据表格选中的id
     */
    getSelectionsIds : function (select){
    	var list = $(select);
    	var sels = list.datagrid("getSelections");
    	var ids = [];
    	for(var i in sels){
    		ids.push(sels[i].id);
    	}
    	ids = ids.join(",");
    	return ids;
    },
    
    /**
     * 初始化单图片上传组件 <br/>
     */
    initOnePicUpload : function(){
    	$(".onePicUpload").click(function(){
			var _self = $(this);
			KindEditor.editor(HX.kingEditorParams).loadPlugin('image', function() {
				this.plugin.imageDialog({
					showRemote : false,
					clickFn : function(url, title, width, height, border, align) {
						var input = _self.siblings("input");
						input.parent().find("img").remove();
						input.val(url);
						input.after("<a href='"+url+"' target='_blank'><img src='"+url+"' width='80' height='50'/></a>");
						this.hideDialog();
					}
				});
			});
		});
    }
};
